/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unc.pds.service;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import trash.UserPrincipal;
import unc.pds.auth.IPDSSecurity;
import unc.pds.auth.SecurityST;
import unc.pds.data.Consts;
import unc.pds.data.RootDataBeanRemote;
import unc.pds.data.RootDataBeanRemoteHome;
import unc.pds.filemodel.AlbumRemoteHome;
import unc.pds.filemodel.FileRemoteHome;
import unc.pds.filemodel.FolderRemoteHome;
import unc.pds.filemodel.PhotoRemoteHome;
import unc.pds.model.CommentRemoteHome;
import unc.pds.model.DiaryRemoteHome;
import unc.pds.model.ModelFactory;
import unc.pds.model.UserRemote;
import unc.pds.model.UserRemoteHome;
import unc.pds.util.Arch;
import unc.pds.util.ArchImpl;

/**
 *
 * @author ain
 */
public class ServiceBean implements SessionBean {

    private SessionContext context;
    private Long sessionKey;
    private String username;

    // <editor-fold defaultstate="collapsed" desc="EJB infrastructure methods. Click the + sign on the left to edit the code.">;
    // TODO Add code to acquire and use other enterprise resources (DataSource, JMS, enterprise bean, Web services)
    // TODO Add business methods or web service operations
    /**
     * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
     */
    public void setSessionContext(SessionContext aContext) {
        context = aContext;
    }

    /**
     * @see javax.ejb.SessionBean#ejbActivate()
     */
    public void ejbActivate() {
    }

    /**
     * @see javax.ejb.SessionBean#ejbPassivate()
     */
    public void ejbPassivate() {
    }

    /**
     * @see javax.ejb.SessionBean#ejbRemove()
     */
    public void ejbRemove() {
    }

    // </editor-fold>;
    /**
     * See section 7.10.3 of the EJB 2.0 specification
     * See section 7.11.3 of the EJB 2.1 specification
     */
    public void ejbCreate(Long key) {
        sessionKey = key;
        // TODO implement ejbCreate if necessary, acquire resources
        // This method has access to the JNDI context so resource aquisition
        // spanning all methods can be performed here such as home interfaces
        // and data sources.
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method" or "Web Service > Add Operation")
    public unc.pds.model.Model getByKey(Long key) throws RemoteException {
        return ModelFactory.getByKey(key, sessionKey);
    }

    public unc.pds.model.Model getByFieldSet(Arch doc, String type) throws RemoteException {
        try {
            return ModelFactory.getByFieldSet(doc, type, sessionKey);
        } catch (Exception e) {
            throw new RemoteException("SerB.getByFieldSet: " + e.getLocalizedMessage());
        }
    }

    /*public unc.pds.model.Model build(unc.pds.util.Arch doc, String type) throws RemoteException {
    try {
    Arch atype = doc.getChild(Consts.ARCH_F_TYPE);
    if (atype == null) {
    doc.addChild(new ArchImpl(Consts.ARCH_F_TYPE, type));
    } else {
    type = atype.getValue();
    }
    if (type.compareTo(Consts.OBJECT_TYPE__USER) == 0) {
    //UserRemoteHome urm = (UserRemoteHome) (new InitialContext()).lookup("ejb/User");
    //return urm.create(doc);
    throw new RemoteException("Use Register for create User");
    }
    if (type.compareTo(Consts.OBJECT_TYPE__DIARY) == 0) {
    DiaryRemoteHome drm = (DiaryRemoteHome) (new InitialContext()).lookup("ejb/Diary");
    return drm.create(doc);
    }
    if (type.compareTo(Consts.OBJECT_TYPE__COMMENT) == 0) {
    CommentRemoteHome drm = (CommentRemoteHome) (new InitialContext()).lookup("ejb/Comment");
    return drm.create(doc);
    }
    } catch (Exception e) {
    throw new RemoteException("Creator: build: " + e.getLocalizedMessage());
    }

    return null;
    }*/
    public unc.pds.model.UserRemote getUser() throws RemoteException {
        try {
            IPDSSecurity sec  = SecurityST.getSecurity(sessionKey);
            return sec.getCurrentUser();
        } catch (FinderException ex) {
            throw new RemoteException("SerB.getUser: " + ex.getLocalizedMessage());
        } catch (CreateException ex) {
            throw new RemoteException("SerB.getUser: " + ex.getLocalizedMessage());
        } catch (NamingException ex) {
            throw new RemoteException("SerB.getUser: " + ex.getLocalizedMessage());
        }
    }

    /*public unc.pds.model.UserRemote getUser2() throws RemoteException {
    UserRemote user;
    if (Consts.DEBUG_MODE) System.out.println("getUser2 "+context.getCallerPrincipal().getName());
    UserPrincipal usrp = (UserPrincipal) context.getCallerPrincipal();

    try {
    user = ((UserRemoteHome) (new InitialContext()).lookup("ejb/User")).create(usrp.getName());
    } catch (Exception e) {
    throw new RemoteException("ERROR: SB: getUser: " + e.getLocalizedMessage());
    }
    return user;
    }*/
}
